package com.css.upms.biz.controller;


import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.css.common.core.utils.R;
import com.css.common.security.utils.SecurityUtil;
import com.css.upms.api.entity.SysMenu;
import com.css.upms.api.entity.SysRoleHome;
import com.css.upms.biz.service.ISysRoleHomeService;
import com.css.upms.biz.service.SysMenuService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pcy
 * @since 2019-06-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/roleHome")
@Api(value = "/roleHome", tags = {"UPMS-角色首页管理"})
@Slf4j
public class SysRoleHomeController {
	
	@Autowired
	private final ISysRoleHomeService sysRoleHomeService;
	
	@Autowired
	private final SysMenuService menuService;

    @ApiOperation(value = "保存角色首页数据")
    @PostMapping("/save")
    public R<SysRoleHome> save(@Valid @RequestBody SysRoleHome req) {
    	log.info("保存角色首页数据");
    	req.setCreateTime(new Date());
    	req.setCreateBy(SecurityUtil.getUser().getId());
    	sysRoleHomeService.saveOrUpdate(req);
        return new R<>(req);
    }
    
    
    @ApiOperation(value = "根据一个角色ID获取首页列表")
    @PostMapping("/getOne")
    public R<SysRoleHome> getOne(@Valid @RequestBody SysRoleHome req) {
    	log.info("获取当前角色配置的首页列表");
    	SysRoleHome roleHome = sysRoleHomeService.getById(req.getRoleId());
        return new R<>(roleHome);
    }
    
    @ApiOperation(value = "根绝当前人的角色获取一个列表")
    @PostMapping("/getHomeList")
    public R<SysMenu> getList() {
    	log.info("获取当前角色配置的首页列表");
    	List<SysRoleHome> homeList = sysRoleHomeService.list(new QueryWrapper<SysRoleHome>().in("ROLE_ID", SecurityUtil.getRoles()).orderByAsc("XH"));
    	SysMenu menu = new SysMenu();
    	if(homeList.size() > 0) {
    		String menuId = homeList.get(0).getMenuId();
    		if(menuId != null && !"".equals(menuId)) {
    			menu = menuService.getById(menuId);
    		}
    	}
    	
    	return new R<>(menu);
    }
    
    
    @SuppressWarnings("rawtypes")
	@ApiOperation(value = "保存角色菜单的时候调用接口-判断是否有菜单删除")
    @PostMapping("/resetMenu/{roleId}")
    public R resetMenu(@PathVariable String roleId) {
    	sysRoleHomeService.resetMenu(roleId);
    	return new R();
    }
	
	
}

